package com.isyscore.vo;

import com.isyscore.constant.ResponseCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: liuxq@isyscore.com
 * @Date: 2021-04-21 15:34:01
 * @Description: 返回结果包装类
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Slf4j
public class ResponseVo<T> {

    /**
     * 响应code,0-处理成功，>0处理失败的各种code，建议有对应code描述列表
     */
    private Integer code;

    /**
     * 响应code对应的描述说明
     */
    private String message;


    /**
     * 响应业务数据，通常为json结构.
     */
    private T data;

    public static ResponseVo<?> success() {
        return ResponseVo.builder().code(ResponseCode.SUCCESS.getCode()).message(ResponseCode.SUCCESS.getMessage()).build();
    }

    public static <T> ResponseVo<T> success(T data) {
        return ResponseVo.<T>builder().code(ResponseCode.SUCCESS.getCode()).message(ResponseCode.SUCCESS.getMessage()).data(data).build();
    }

    public static <T> ResponseVo<T> success(String message, T data) {
        return ResponseVo.<T>builder().code(ResponseCode.SUCCESS.getCode()).message(ResponseCode.SUCCESS.getMessage()).data(data).build();
    }

    public static <T> ResponseVo<T> success(String message) {
        return ResponseVo.<T>builder().code(ResponseCode.SUCCESS.getCode()).message(message).build();
    }

    public static ResponseVo<?> fail() {
        return ResponseVo.builder().code(ResponseCode.FAIL.getCode()).message(ResponseCode.FAIL.getMessage()).build();
    }

    public static <T> ResponseVo<T> fail(T data) {
        return ResponseVo.<T>builder().code(ResponseCode.FAIL.getCode()).message(ResponseCode.FAIL.getMessage()).data(data).build();
    }

    public static <T> ResponseVo<T> fail(String message) {
        return ResponseVo.<T>builder().code(ResponseCode.FAIL.getCode()).message(message).build();
    }


    public static <T> ResponseVo<T> fail(String message, T data) {
        return ResponseVo.<T>builder().code(ResponseCode.FAIL.getCode()).
                message(message)
                .data(data).build();
    }

    public static <T> ResponseVo<T> fail(ResponseCode code, T data) {
        return ResponseVo.<T>builder().code(code.getCode()).message(code.getMessage()).data(data).build();
    }

    public static <T> ResponseVo<T> fail(ResponseCode code) {
        return ResponseVo.<T>builder().code(code.getCode()).message(code.getMessage()).build();
    }
}